//
//  NSDate+JYDateCul.m
//  BaiSi
//
//  Created by UJY on 2016/12/15.
//  Copyright © 2016年 谭占武. All rights reserved.
//

#import "NSDate+JYDateCul.h"

@implementation NSDate (JYDateCul)

-(BOOL)isToday{
    NSCalendar *cal = [NSCalendar currentCalendar];
    return [cal isDateInToday:self];
}

-(BOOL)isYesterday{
    NSCalendar *cal = [NSCalendar currentCalendar];
    return [cal isDateInYesterday:self];
}

-(BOOL)isYear{
    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *currCom = [cal components:NSCalendarUnitYear fromDate:[NSDate date]];
    NSDateComponents *selfCom = [cal components:NSCalendarUnitYear fromDate:self];
    return currCom.year == selfCom.year;
}

-(NSDateComponents *)dateDetal{
    NSCalendar *cal = [NSCalendar currentCalendar];
    return [cal components:NSCalendarUnitHour|NSCalendarUnitMinute fromDate:self toDate:[NSDate date] options:NSCalendarWrapComponents];
}

-(NSString *)culTimeWithString:(NSString *)str{

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    dateFormat.dateFormat = @"yyyy-MM-dd HH-mm-ss";
    NSDate *creatDate = [dateFormat dateFromString:str];
    if ([creatDate isYear]) {
        if ([creatDate isToday]) {
            NSDateComponents *com = [creatDate dateDetal];
            if (!com.hour) {
                if (com.minute > 1) {
                    return [NSString stringWithFormat:@"%zd分钟前",com.minute];
                }else{
                    return @"刚刚";
                }
            }else{
                return [NSString stringWithFormat:@"%zd小时前",com.hour];
            }
        }else if ([creatDate isYesterday]){
            return @"昨天";
        }else{
            dateFormat.dateFormat = @"MM-dd HH-mm-ss";
            NSString *str = [dateFormat stringFromDate:creatDate];
            return str;
        }
    }else{
        return str;
    }
    
}


@end
